#!/usr/bin/env python
# coding: utf-8

# ## 实践 face++ 人脸检测/face detect

# In[54]:


import requests


# In[55]:


# 1. url
base_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'

# 2. api账户/通行证
API_key = 'MDWXHz-Sd9lPFfSFONGpV992nhJj71iq'
API_secret = '2H6Tmh9MhwnUu-VVz56w81x4CD71yzkw'

# 3. POST


# 4. 所有 API Key 都可以调用本 API。
#    其中 calculate_all 和 face_rectangle 参数只有正式 API Key 才能使用，试用 API Key 无法使用。

# 5. payload： 酬载, 必要阅读api文档中的 *必选项* 和 *可选性*，及 *参数说明*
payload = {
    'api_key':API_key,
    'api_secret':API_secret,
     'image_url':'https://tse1-mm.cn.bing.net/th/id/OIP-C.HPWFQ7UDQh93EKcDmiUQ_wHaIR?w=176&h=197&c=7&r=0&o=5&dpr=2&pid=1.7' 
}

r = requests.post(base_url, params = payload)


# In[56]:


r


# In[57]:


r.json()


# In[58]:


face_detect = r.json()


# In[59]:


face_tokens = face_detect['faces'][0]['face_token']


# In[61]:


face_tokens


# ## 人脸分析

# In[62]:


import requests


# In[63]:


analyze_base_url = 'https://api-cn.faceplusplus.com/facepp/v3/face/analyze'

API_key = 'MDWXHz-Sd9lPFfSFONGpV992nhJj71iq'
API_secret = '2H6Tmh9MhwnUu-VVz56w81x4CD71yzkw'

payload = {
    'api_key':API_key,
    'api_secret':API_secret,
    'face_tokens':'ba4c1937ba1b6314f27bd5d233bc0e94',
    'return_attributes':'gender,age,smiling,headpose,facequality,blur,eyestatus,emotion,beauty,mouthstatus,eyegaze,skinstatus',
    'beauty_score_min':0,
    'beauty_score_max':100
  
}
r_analyze = requests.post(url = analyze_base_url, params = payload)


# In[64]:


r_analyze


# In[65]:


r_analyze.json()


# In[66]:


import pandas as pd


# In[67]:


pd.json_normalize(r_analyze.json()['faces']).T


# ## 人脸对比

# In[2]:


from PIL import Image
Image.open('D:\yyqx.jpg')


# In[6]:


print(open('D:\yyqx.jpg','rb'))


# In[7]:


import requests
API_key = 'MDWXHz-Sd9lPFfSFONGpV992nhJj71iq'
API_secret = '2H6Tmh9MhwnUu-VVz56w81x4CD71yzkw'


# In[11]:


def face_detect(API_key,API_secret,image_path):
    """该函数为调用face++ face_detect接口"""
    base_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'
    files = {'image_file':('D:\yyqx.jpg',open('D:\yyqx.jpg','rb'),'mutipart/form-data')}
    payload = {
        'api_key':API_key,
        'api_secret':API_secret
    }
    r = requests.post(base_url,params = payload,files=files )
    return r.json()


# In[12]:


face_detect(API_key,API_secret,'D:\yyqx.jpg')


# ## 准备人脸元数据

# In[13]:


face_detect(API_key,API_secret,'D:\yyqx.jpg')


# In[14]:


yyqx_meta_data = face_detect(API_key,API_secret,'D:\yyqx.jpg')


# In[15]:


yyqx_meta_data_face_token = yyqx_meta_data['faces'][0]['face_token']
yyqx_meta_data_face_token


# ## 给出人脸图片进行相似度查询

# In[16]:


face_detect(API_key,API_secret,'D:\yyqx.jpg')


# In[17]:


face02_token = face_detect(API_key,API_secret,'D:\yyqx.jpg')['faces'][0]['face_token']
face02_token


# In[18]:


compare_url = 'https://api-cn.faceplusplus.com/facepp/v3/compare'

payload = {
    'api_key':API_key,
    'api_secret':API_secret,
    'face_token1':yyqx_meta_data_face_token,
    'face_token2':face02_token
}
r = requests.post(compare_url,params = payload)


# In[19]:


r.json()


# ## 人脸检测函数

# In[1]:


API_key = 'MDWXHz-Sd9lPFfSFONGpV992nhJj71iq'
API_secret = '2H6Tmh9MhwnUu-VVz56w81x4CD71yzkw'


# In[10]:


def face_detect(API_key,API_secret,image_url,landmark=None,attributes=None):
    """face++ 人脸检测接口函数(图片链接)"""
    import requests
    url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'
    payload={
        'api_key':API_key,
        'api_secret':API_secret,
        'image_url':image_url,
        'return_landmark':landmark,
        'return_attributes':attributes
    }
    r = requests.post(url,params=payload)
    return r.json()


# In[11]:


images = face_detect(API_key,API_secret,"https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fdingyue.nosdn.127.net%2FGLi6LXsaRyDLT0nW8UC3ZqKAcZQiHKXaBev30EgdsheTQ1545017642482compressflag.jpg&refer=http%3A%2F%2Fdingyue.nosdn.127.net&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1650516786&t=93bdaee3887452df34a0aef331abce41")


# In[12]:


images["faces"][0]["face_token"]


# ## 人脸库API组

# In[15]:


import requests
API_key = 'MDWXHz-Sd9lPFfSFONGpV992nhJj71iq'
API_secret = '2H6Tmh9MhwnUu-VVz56w81x4CD71yzkw'
base_url = 'https://api-cn.faceplusplus.com/facepp/v3/faceset/create'


# In[16]:


payload = {
    'api_key':API_key,
    'api_secret':API_secret,
    'face_tokens':'b7104490396ceacafbab449f348e5b7f,fc3cec67abb7df0fc186915ba9b964f3,5c2bf50d263e64feccad3ae286ec6e81'
}


# In[17]:


r = requests.post(base_url,params=payload)


# In[18]:


r


# In[19]:


r.json()


# ## 人脸搜索

# In[20]:


import requests
API_key = 'MDWXHz-Sd9lPFfSFONGpV992nhJj71iq'
API_secret = '2H6Tmh9MhwnUu-VVz56w81x4CD71yzkw'
base_url = 'https://api-cn.faceplusplus.com/facepp/v3/search'


# In[21]:


payload = {
    'api_key':API_key,
    'api_secret':API_secret,
    'image_url':'https://tse1-mm.cn.bing.net/th/id/OIP-C.m058ka_wvhohUxj-Xvt_6gHaJr?w=203&h=265&c=7&r=0&o=5&dpr=1.5&pid=1.7',
    'faceset_token': '2a0efc8a42b3269dc8fe0df9b39c5040'
}


# In[22]:


r = requests.post(base_url,params=payload)


# In[23]:


r


# In[24]:


r.json()


# In[ ]:




